package com.ganmiao.server.sys.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ganmiao.server.supplier.vo.InformationVo;
import com.ganmiao.server.sys.entity.SysUser;
import com.ganmiao.server.sys.query.SysUserQuery;
import com.ganmiao.server.sys.vo.SysUserVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户信息
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {

    /**
     * 获取用户信息(分页)
     *
     * @param iPage
     * @param sysUserQuery
     * @return
     */
    @Select({
            "<script>",
            "select `user`.user_id,`user`.user_name,`user`.user_sex,`user`.user_phone,`user`.birth_date,`user`.identity_card,`user`.native_place,department.department_name,department.department_id",
            "from sys_user `user` left join sys_department department on `user`.department_id = department.department_id and department.status = 1",
            "where `user`.status = 1",
            "<if test = 'sysUserQuery.userName!=null'>",
            "and `user`.user_name like concat('%',#{sysUserQuery.userName},'%')",
            "</if>",
            "<if test = 'sysUserQuery.sysCode!=null'>",
            "and `user`.sys_code = #{sysUserQuery.sysCode}",
            "</if>",
            "<if test = 'sysUserQuery.supplierId!=null'>",
            "and `user`.user_id in (select user_id from supplier_user where supplier_id = #{sysUserQuery.supplierId} and status = 1)",
            "</if>",
            "<if test = 'sysUserQuery.shopId!=null'>",
            "and `user`.user_id in (select user_id from shop_user where shop_id = #{sysUserQuery.shopId} and status = 1)",
            "</if>",
            "order by `user`.created_time desc",
            "</script>"
    })
    IPage<SysUserVo> getSysUserList(@Param("iPage") IPage iPage, @Param("sysUserQuery") SysUserQuery sysUserQuery);

    /**
     * 获取用户信息(分页)
     *
     * @param iPage
     * @param sysUserQuery
     * @return
     */
    @Select({
            "<script>",
            "select `user`.user_id,`user`.user_name,`user`.user_sex,`user`.user_phone,`user`.birth_date,`user`.identity_card,`user`.native_place,department.department_name,department.department_id",
            "from sys_user `user` left join sys_department department on `user`.department_id = department.department_id and department.status = 1",
            "where `user`.status = 1 ",
            "and `user`.sys_code = #{sysUserQuery.sysCode} ",
            "and `user`.user_id in (select user_id from shop_user where shop_id = #{sysUserQuery.shopId} and status = 1)",
            "<if test = 'sysUserQuery.userName!=null'>",
            "and `user`.user_name like concat('%',#{sysUserQuery.userName},'%')",
            "</if>",
            "order by `user`.created_time desc",
            "</script>"
    })
    IPage<SysUserVo> shopSysUserList(@Param("iPage") IPage iPage, @Param("sysUserQuery") SysUserQuery sysUserQuery);

    //获取用户信息(详情)
    @Select({
            "select user.user_id,user.user_name,user.user_sex,user.birth_date,user.user_phone,user.identity_card,user.department_id,user.native_place,user.head_pic_id,account.status,account.user_account,account.user_password,account.sys_code",
            "from sys_user user",
            "join sys_user_account account on user.user_id = account.user_id",
            "where user.user_id = #{userId}"
    })
    SysUserVo getSysUserById(Integer userId);

    //获取个人信息
    @Select({
            "select user.user_id,user.user_name,user.user_sex,user.birth_date,user.user_phone,user.identity_card,user.department_id,user.native_place,user.head_pic_id,account.status,account.user_account,account.user_password,account.sys_code",
            "from sys_user user",
            "join sys_user_account account on user.user_id = account.user_id",
            "where user.user_id = #{userId}"
    })
    InformationVo getInformationById(Integer userId);

}
